package com.hontech.bread.ui

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.hontech.bread.R
import com.hontech.bread.app.globalInit
import com.hontech.bread.device.MenuClickEvent
import com.hontech.bread.ui.fragment.Rsc3Fragment
import com.hontech.bread.ui.fragment.Test3Fragment
import com.hontech.bread.ui.fragment.Vend2Fragment
import com.hontech.bread.utils.EmptyEdge
import com.hontech.bread.utils.Height
import com.hontech.bread.utils.Width
import com.hontech.bread.utils.isBigScreen
import com.hontech.bread.utils.log
import com.hontech.bread.utils.registerEvent
import com.hontech.bread.utils.runOnUi
import com.hontech.bread.utils.unregister
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class MainActivity : BaseActivity() {

    private lateinit var mTabInfo: TabInit

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        log("width:$Width height:$Height")
        mTabInfo = TabInit(this)
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            hideButton()
            runOnUi { globalInit() }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onClickMenu(_s: MenuClickEvent) {
        mTabInfo.vendCtrl()
    }

    override fun onStart() {
        super.onStart()
        registerEvent(this)
    }

    override fun onStop() {
        super.onStop()
        unregister(this)
    }

}


private class TabInfo(
    val text: String,
    val iconId: Int,
    val fragment: Fragment
)


private class TabInit(activity: MainActivity) : FragmentStateAdapter(activity) {

    private var mVendFlag = false
    private val mVp2: ViewPager2
    private val mTable: TabLayout

    private val tabInfos = arrayOf(
        TabInfo("Sale", R.drawable.ic_sale, Vend2Fragment()),
        //  TabInfo("Sale2", R.drawable.ic_sale, Vend3Fragment()),
        TabInfo("Replenish", R.drawable.ic_replenish, Rsc3Fragment()),
        TabInfo("Test", R.drawable.ic_test, Test3Fragment()),
    )

    init {
        val vp2 = activity.findViewById<ViewPager2>(R.id.id_main_vp2)
        val tabs = activity.findViewById<TabLayout>(R.id.id_main_tablayout)
        mVp2 = vp2
        mTable = tabs

        vp2.adapter = this
        vp2.offscreenPageLimit = tabInfos.size
        TabLayoutMediator(tabs, vp2, true, false) { tab, index ->
            tab.text = tabInfos[index].text
            tab.setIcon(tabInfos[index].iconId)
        }.attach()
        vp2.isUserInputEnabled = false
        (vp2.getChildAt(0) as RecyclerView).edgeEffectFactory = EmptyEdge()

        if (isBigScreen()) {
            vp2.currentItem = 1
        } else {
            vp2.currentItem = 0
        }

        vendCtrl()
    }

    fun vendCtrl() {
        mVendFlag = !mVendFlag

        if (mVendFlag) {
            //   mVp2.currentItem = 0
            mTable.visibility = View.GONE
        } else {
            mTable.visibility = View.VISIBLE
        }
    }

    override fun getItemCount(): Int {
        return tabInfos.size
    }

    override fun createFragment(position: Int): Fragment {
        return tabInfos[position].fragment
    }
}
